package com.example.highway.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.highway.config.MyJson;
import com.example.highway.dao.InspectorAdminDao;
import com.example.highway.dao.InspectorDao;
import com.example.highway.dao.OwnerInspectorApealDao;
import com.example.highway.entity.Car;
import com.example.highway.entity.Inspector;
import com.example.highway.entity.InspectorAdmin;
import com.example.highway.entity.OwnerInspectorApeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

@RestController
@CrossOrigin
@RequestMapping("inspector")
public class InspectorController {
    @Autowired
    InspectorDao inspectorDao;
    @Autowired
    OwnerInspectorApealDao ownerInspectorApealDao;

    @RequestMapping("add")
    public MyJson add(@RequestBody() String msg){
//        JSONObject jsonObject = JSON.parseObject(msg);
//
//        Optional<Inspector> o = inspectorDao.findById((String)jsonObject.get("IID"));
//        if(o.isPresent()){
//            return new MyJson("0", false);
//        }
//
//        Inspector c = new Inspector();
//        c.setIID((String)jsonObject.get("AID"));
//        c.setPassword((String)jsonObject.get("password"));
//        c.setPhone((String)jsonObject.get("phone"));
//        c.setReport((String)jsonObject.get("report"));
//        inspectorDao.save(c);
//
//        return new MyJson("0", true);
        JSONObject jsonObject = JSON.parseObject(msg);
        Inspector inspector;
        try {
            Optional<Inspector> o = inspectorDao.findById((String)jsonObject.get("IID"));
            if(o.isPresent()){
                JSONObject rtn = new JSONObject();
                rtn.put("msg", "existed IID");
                return new MyJson("1", rtn);
            }
            inspector = inspectorDao.save(jsonObject.toJavaObject(Inspector.class));
        } catch (Exception e){
            JSONObject rtn = new JSONObject();
            String errMsg = "lack of key";
            rtn.put("msg", errMsg);
            return new MyJson("1", rtn);
        }

        JSONObject rtn = new JSONObject();
        rtn.put("IID", inspector.getIID());
        return new MyJson("0", rtn);
    }

    @RequestMapping("get")
    public MyJson one(@RequestBody() String msg){
        JSONObject jsonObject = JSON.parseObject(msg);

//        Optional<Inspector> o = inspectorDao.findById((String)jsonObject.get("IID"));
//        if(!o.isPresent()){
//            return new MyJson("0", null);
//        }
//        Inspector inspector = o.get();

        Inspector inspector = jsonObject.toJavaObject(Inspector.class);
        List<Inspector> inspectors = inspectorDao.findAll(Example.of(inspector));

        return new MyJson("0", JSONArray.toJSON(inspectors));

    }

    @RequestMapping("update")
    public MyJson update(@RequestBody() String msg){
        JSONObject jsonObject = JSON.parseObject(msg);
        if(!jsonObject.containsKey("IID")){
            JSONObject rtn = new JSONObject();
            rtn.put("msg", "lack of IID");
            return new MyJson("1", rtn);
        }

        try {
            Inspector inspector = inspectorDao.save(jsonObject.toJavaObject(Inspector.class));
            return new MyJson("0", JSONObject.toJSON(inspector));
        } catch (Exception e){
            JSONObject rtn = new JSONObject();
            String errMsg = "lack of key";
            rtn.put("msg", errMsg);
            return new MyJson("1", rtn);
        }
    }

    /**
     * 完成稽查管理员安排的稽查任务;
     * @param msg json{id 稽查任务id, status 任务状态, IID 稽查员id}
     * @return json
     */
    @RequestMapping("appeal")
    public MyJson appeal(@RequestBody() String msg){
        JSONObject jsonObject = JSON.parseObject(msg);
        if(!jsonObject.containsKey("status") || !jsonObject.containsKey("id") || !jsonObject.containsKey("IID")){
            JSONObject rtn = new JSONObject();
            rtn.put("msg", "lack of key");
            return new MyJson("1", rtn);
        }
        if((Integer)jsonObject.get("status") != 1 && (Integer)jsonObject.get("status") != 2){
            JSONObject rtn = new JSONObject();
            rtn.put("msg", "invalid status");
            return new MyJson("1", rtn);
        }

        Optional<OwnerInspectorApeal> optional;
        optional = ownerInspectorApealDao.findById((Integer) jsonObject.get("id"));
        if(!optional.isPresent() || !optional.get().getIID().equals(jsonObject.getString("IID"))){
            JSONObject rtn = new JSONObject();
            rtn.put("msg", "appeal not found");
            return new MyJson("1", rtn);
        }
        OwnerInspectorApeal ownerInspectorApeal = optional.get();
        ownerInspectorApeal.setStatus((Integer)jsonObject.get("status"));
        ownerInspectorApeal = ownerInspectorApealDao.save(ownerInspectorApeal);

        return new MyJson("0", JSONObject.toJSON(ownerInspectorApeal));


    }
//    @RequestMapping("get-all")
//    public MyJson all(){
//        List<Inspector> o = inspectorDao.findAll();
//        if(o.isEmpty()){
//            return new MyJson("0", null);
//        }
//
//        return new MyJson("0", o);
//
//    }
}
